import socket
import urllib2
import re
import threading
import Queue

d={"z":"3","j":"4","r":"2","l":"9","c":"0","x":"5","i":"7","a":"6","p":"8","s":"1"}

socket.setdefaulttimeout(45)

class ProxyChecker(threading.Thread):
	def __init__(self, proxy_queue, ok_queue):
		threading.Thread.__init__(self)
		self.proxy_queue = proxy_queue
		self.ok_queue = ok_queue
		
	def run(self):
		while 1:
			try:
				pip = self.proxy_queue.get_nowait()
				if self.is_bad_proxy(pip):
					print "Bad Proxy", pip
				else:
					print pip, "is working"
					self.ok_queue.put(pip)
			except Queue.Empty,e:
				break
			except:
				pass

	def is_bad_proxy(self, pip):
		try:        
			proxy_handler = urllib2.ProxyHandler({'http': pip})
			opener = urllib2.build_opener(proxy_handler)
			opener.addheaders = [('User-agent', 'Mozilla/5.0')]
			urllib2.install_opener(opener)
			req=urllib2.Request('http://www.google.com')  # change the url address here
			sock=urllib2.urlopen(req)
			sock=urllib2.urlopen(req)
			print sock.url
			if sock.url.startswith('http://www.google'):
				c=sock.read()
				if c.find('<title>Google</title>')!=-1:
					return 0
				else:
					return 1
			else:
				return 1
		except urllib2.HTTPError, e:        
			print 'Error code: ', e.code
			return e.code
		except Exception, detail:
			print "ERROR:", detail
			return 1
	
def translate(ps):
 	p=''
 	for c in ps:
 		if c in d:
 			p+=d[c]
 	return p

def get_proxy_list():
    fp=open('proxy.txt', 'wb+')

    for i in range(1,11):
        url='http://www.cnproxy.com/proxy%d.html' % i
        r=urllib2.urlopen(url)
        c=r.read()
        ips=re.findall(r'<td>(\d+\.\d+\.\d+\.\d+)', c)
        ps=map(translate, re.findall(r'''write\(":"(.+?)\)''', c))
        for ip, port in zip(ips,ps):
            fp.write('%s:%s\r\n'%(ip,port))
            print '.',
            
    fp.close()

def test_proxy(thread_num = 10):
	proxy_queue = Queue.Queue()
	ok_queue = Queue.Queue()

	f=open('proxy.txt')
	for l in f:
		l = l.strip()
		if l:
			proxy_queue.put(l)
	f.close()

	threads = []
	for i in range(thread_num):
		threads.append(ProxyChecker(proxy_queue, ok_queue))

	for t in threads:
		t.start()

	f=open('proxy_ok.txt', 'wb+')
	f.close()
	while 1:
		try:
			pip = ok_queue.get(timeout=60)
			f=open('proxy_ok.txt', 'ab+')
			f.write(pip+'\r\n')
			f.close()
		except Queue.Empty,e:
			break
		except:
			pass

	for t in threads:
		t.join()
		
def check_proxy():
    get_proxy_list()
    test_proxy()

if __name__=='__main__':
	check_proxy()    
